﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeoSketch2.Core;
using System.IO;
using System.Xml;
using System.Drawing;

namespace GeoSketch2.Archaeo
{
    /// <summary>
    /// The geotagged image insertion tool
    /// </summary>
    public class TGeoTaggedImage: BaseArchaeoTool
    {
        #region Fields
        ImagePositionningBox ipb;
        GeoTaggedImage GeoTaggedImg; 
        #endregion

        #region Constructor
        /// <summary>
        /// The tool to draw geo tagged images objects
        /// </summary>
        /// <param name="Viewport">The viewport to draw to</param>
        public TGeoTaggedImage(Viewport Viewport)
            : base(Viewport,"GeoTagged Image")
        {
            PictureManagerForm pmf = new PictureManagerForm(Viewport.Project.ProjectDirectory + "\\Images");
            pmf.ShowDialog();
            if (pmf.SelectedPictureFullFilename != "")
            {
                if (File.Exists(Path.GetDirectoryName(pmf.SelectedPictureFullFilename) + "\\" + Path.GetFileNameWithoutExtension(pmf.SelectedPictureFullFilename) + ".gsp"))
                {
                    GeoTaggedImg = new GeoTaggedImage(pmf.SelectedPictureFullFilename, Core.FileIO.ReadGSP(pmf.SelectedPictureFullFilename), Viewport.CurrentLayer, null);
                    Viewport.GeoObjects.Add(GeoTaggedImg);
                }
                else
                {
                    GeoTaggedImg = new GeoTaggedImage(pmf.SelectedPictureFullFilename, new RectangleD(), Viewport.CurrentLayer, null);
                    ipb = new ImagePositionningBox(GeoTaggedImg.Image, this);
                    ipb.Top = (Viewport.DisplayControl.Height - ipb.Height) / 2;
                    ipb.Left = (Viewport.DisplayControl.Width - ipb.Width) / 2;
                    ipb.Parent = Viewport.DisplayControl;
                    ipb.Show();
                }
            }
        } 
        #endregion

        #region Inputs
        /// <summary>
        /// The method called when a text is sent to the tool
        /// </summary>
        public override void TextEntry(string Text)
        {
            if (Text == "OK")
            {
                GeoTaggedImg.ImageExtents = Viewport.Object2ImageTransformation.InverseTransform(ipb.Bounds, YAxisDirection.UpSide);
                Viewport.GeoObjects.Add(GeoTaggedImg);
                ipb.Dispose();
                RefreshViewport();
            }
        }
        #endregion
    }
}